$(function () {
    //全局定义页数与总条数
    let markid = getUrlString('markid');
    let key = getUrlString('key');
    // let markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    let page = -1,totalSize = 0,sort= 'time',sptypes = '',maintypes = '',maxmoney = '99999',minmoney = '1',filter = 1,ishowgold = 0;
    $(document).on('click', '.all', function() {
        let buttons1 = [
            {
                text: '全部',
                bold: true,
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('全部<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    totalSize = 0;
                    minmoney = '1';
                    maxmoney = '999999';
                    sptypes = "";
                    maintypes = '';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            },
            {
                text: '现货',
                bold: true,
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('现货<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    totalSize = 0;
                    maintypes = '';
                    minmoney = '1';
                    maxmoney = '999999';
                    sptypes = '0,1,3,4,5,6,7,8';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            },
            {
                text: '预售',
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('预售<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    sptypes = '2';
                    maintypes = '';
                    minmoney = '1';
                    maxmoney = '999999';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            },
            // {
            //     text: '银币商品',
            //     onClick: function () {
            //         $('.option').removeClass('check');
            //         $('#screenImg').attr('src','../../res/img/common/sx.png');
            //         $('.screen').removeClass('common');
            //         $(".all").html('银币商品<img src="../../res/img/common/downJt.png" />');
            //         $('#usedList').empty();//清空容器
            //         mRefresh.resetUpLoading(true);
            //         page = 0;
            //         sptypes = '5';
            //         maintypes = '';
            //         loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
            //     }
            // },
            {
                text: '返回',
                onClick: function () {

                }
            }];
        $.modal({
            title: '请选择品类',
            verticalButtons: true,
            buttons:buttons1
        });
    });

    $('#jsbm').click(function () {
        window.location.href = 'dancool://web='+urlView+'consignmentSales/consignment.html?type=0'
    });

    $('.priceSel').click(function () {
        $('#usedList').empty();//清空容器
        // mRefresh.resetUpLoading(true);
        let thisHtml = $(this).find('span').html();
        totalSize = 0;
        if(thisHtml === '价格升序'){
            sort = 'moneydesc';
            page = -1;
            $('.newsBest').removeClass('common');
            $(this).find('span').addClass('common').html('价格降序');
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
        }else {
            sort = 'money';
            page = -1;
            $('.newsBest').removeClass('common');
            $(this).find('span').addClass('common').html('价格升序');
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
        }
    });

    $('.newsBest').click(function () {
        $('#usedList').empty();//清空容器
        // mRefresh.resetUpLoading(true);
        sort = 'time';
        $('.selectCom span').removeClass('common');
        $(this).addClass('common');
        page = -1;
        totalSize = 0;
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
    });

    //筛选模块
    $('.screen').click(function () {
        $('.screenIndex').show().removeClass('bounceOutRight').addClass('bounceInRight');
        $(".indexAlert").css("height",$(".alertContent").height())
    });
    $('.commodityType .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
        }else {
            $(this).addClass('check');
        }
    });

    $('.commodityAttribute .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
        }else {
            $(this).addClass('check');
        }
    });

    $('.commodityEncryption .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
            filter = 1
        }else {
            $(this).addClass('check');
            filter = 0
        }
    });

    $('.buyType .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
            $(this).attr('optioncode','0');
        }else {
            $(this).addClass('check');
            $(this).attr('optioncode','1');
        }
    });

    $('.confirm').click(function () {
        $(".all").html('全部<img src="../../res/img/common/downJt.png" />');
        page = 0;
        totalSize = 0;
        let minMoney = $('#minmoney').val();
        let maxMoney = $('#maxmoney').val();
        if(Number(minMoney) > Number(maxMoney)){
            $.toast('最低价不能大于最高价');
            return
        }

        if(maxMoney > '99999'){
            $.toast('最高价不能大于99999');
            return
        }
        let commodityTypeArr = $(".commodityType .check");
        let commodityTypeArrPush = [];
        commodityTypeArr.each(function(){
            commodityTypeArrPush.push($(this).attr("optionCode"));
        });
        let commodityTypeStr = commodityTypeArrPush.toString(',');
        let commodityAttributeArr = $(".commodityAttribute .check");
        let commodityAttributeArrPush = [];
        commodityAttributeArr.each(function(){
            commodityAttributeArrPush.push($(this).attr("optionCode"));
        });
        let commodityAttributeStr = commodityAttributeArrPush.toString(',');
        if(commodityTypeStr === '' && commodityAttributeStr === '' && minMoney === '' && maxMoney === ''){
            $('#screenImg').attr('src','../../res/img/common/sx.png');
            $('.screen').removeClass('common');
        }else {
            $('.screen').addClass('common');
            $('#screenImg').attr('src','../../res/img/common/sxSec.png');
        }
        if(minMoney === ''){
            minmoney = '1'
        }else {
            minmoney = minMoney
        }

        if(maxMoney === ''){
            maxmoney = '99999'
        }else {
            maxmoney =maxMoney
        }
        $('#usedList').empty();//清空容器
        sptypes = commodityTypeStr;
        maintypes = commodityAttributeStr;
        let buyTypeoption = $('.buyTypeoption').attr('optionCode');
        let buyTypeoption2 = $('.buyTypeoption2').attr('optionCode');
        if (buyTypeoption === '1' && buyTypeoption2 === '1'){
            ishowgold = 0
        } else if (buyTypeoption === '1'){
            ishowgold = 1
        } else if (buyTypeoption2 === '1'){
            ishowgold = 2
        }else {
            ishowgold = 0
        }
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key);
        $('.screenIndex').removeClass('bounceInRight').addClass('bounceOutRight');
    });

    $('.reset').click(function () {
        $('.option').removeClass('check');
        $('#usedList').empty();//清空容器;
        $('#screenImg').attr('src','../../res/img/common/sx.png');
        $('.screen').removeClass('common');
        sptypes = '';
        maintypes = '';
        totalSize = 0;
        page = 0;
        ishowgold = 0;
        minmoney = '1';
        maxmoney = '999999';
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key);
    });

    $('.indexAlert').click(function () {
        $('.screenIndex').removeClass('bounceInRight').addClass('bounceOutRight');
    });

    let mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#usedList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
            }
        }
    });

    function loadpagedata (minmoney,maxmoney,page,sptypes,maintypes,sort,key) {
        let data = {
            'markid':markid,
            'minmoney':minmoney,
            'maxmoney':maxmoney,
            'page':page,
            'sptypes':sptypes,
            'maintypes':maintypes,
            'sort':sort,
            'key':key,
            'filter':filter,
            'ishowgold':ishowgold
        };
        getData('post','/niu/apiv3/xiandanv2_getlist.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                let append = '';
                if(data.pagetotal === 0){
                    mRefresh.endUpLoading(true);
                    $('.minirefresh-hardware-speedup').hide();
                    $('.nullContent').show();
                    return
                }else {
                    $('.nullContent').hide();
                    $('.minirefresh-hardware-speedup').show();
                }
                // if(data.data === null){
                //
                // }else {
                //
                // }
                $(data.data).each(function () {
                    if(this.showtype === 'xiandan'){
                        append += '<li class="XDItem">' +
                            '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                            '                   <input type="hidden" class="toyscount" value="'+this.toyscount+'"> '+
                            '                   <input type="hidden" class="toysid" value="'+this.toysid+'"> '+
                            '                    <div class="pic">' +
                            '                        <img src="'+this.pics+'" alt="">' +
                            '                    </div>' ;
                        if (this.bukuan_status === '0'){
                            append += '                    <div class="name"><img src="../../res/img/dj2.png" alt=""><span>'+this.name+'</div>' ;
                        }else {
                            append += '                    <div class="name"><span>'+this.name+'</span></div>' ;
                        }
                        if (this.ispw === '1'){
                            append +='<div class="leftTop">密码</div>'
                        }
                        append += '          <div class="price">' ;
                        if (this.isshow === '1'){
                            append +=   ' <span style="color: #ff0000;font-weight: 600" class=goldCoinNum>￥'+(this.moneyrmb)/100+'</span>' ;
                        } else {
                            append +=   '                        <img class="goldCoin" src="../../res/img/common/goldCoin.png" alt="">' +
                                '                        <span class=goldCoinNum>'+this.money2+'</span>' ;
                        }
                         append +=
                            '                    </div>'+
                            '                </li>'
                    }
                    // else {
                    //
                    //     append += '<li class="HPItem">' +
                    //         '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                    //         '                   <input type="hidden" class="showtype" value="'+this.showtype+'"> '+
                    //         '                    <div class="pic">' +
                    //         '                        <img src="'+this.picbanner+'" alt="">' +
                    //         '                    </div>' +
                    //         '                    <div class="name">'+this.name+'</div>' +
                    //         '                    <div class="price1">' +
                    //         '                        <img class="goldCoin" src="../../res/img/common/HP.png" alt="">' +
                    //         '                        <span class=goldCoinNum>'+this.money3+'</span>' +
                    //         '                    </div>' +
                    //         // '                    <div class="info1">可用'+this.money2+'金币代付</div>' +
                    //         '                </li>'
                    // }

                });
                $('#usedList').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }

                if (page !== -1){
                    totalSize = totalSize + 10;
                }

                console.log(totalSize)
                if (totalSize > data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        })
    }

    $("#usedList .XDItem").live("click", function(){
        let id = $(this).find('.id').val();
        let toyscount = $(this).find('.toyscount').val();
        let toysid = $(this).find('.toysid').val();
        if (toyscount === '1'){
            window.location.href = 'dancool://fullweb='+urlView+'saltedEggV2/saltedDetail.html?id='+id;
        }else {
            // window.location.href = 'saltedEggGettoylist.html?toysid='+toysid
            window.location.href="dancool://web="+urlView+"saltedEggV2/saltedEggGettoylist.html?toysid="+toysid;
        }
    });

    $("#usedList .HPItem").live("click", function(){
        let id = $(this).find('.id').val();
        window.location.href = 'dancool://fullweb='+urlView+'HPStore/HPStoreDetail.html?poolid='+id+'&markid='+markid
        // window.location.href = 'dancool://hpid='+id;
    });
});
